package com.yypx.common.utils;

import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class ObjToMapUtil {
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static ObjToMapUtil instance = null;

	public static Object toHashMap(Object obj) {
		if (instance == null)
			instance = new ObjToMapUtil();
		return instance.getMapListObject(obj);
	}

	public Object getMapListObject(Object bean) {
		if (bean == null)
			return null;
		Class clazz = bean.getClass();
		if (bean instanceof Number) {
			/*
			 * if (bean instanceof Double) { return
			 * StringUtil.doubleToString(Double.valueOf(bean.toString())); }
			 * else
			 */
			if (bean instanceof Integer) {
				return String.valueOf(bean);
			} else
				return bean;
		}
		if (bean instanceof Boolean || bean instanceof String)
			return bean;
		if (bean instanceof Date) {
			String datetime = dateFormat.format(bean);
			if (datetime.lastIndexOf("00:00:00") >= 0)
				datetime = datetime.replace("00:00:00", "");
			return datetime;
		}
		if (bean instanceof java.util.UUID) {
			return ((java.util.UUID) bean).toString();
		}
		if (clazz.isArray()) {
			ArrayList<Object> arrayList = new ArrayList<Object>();
			int arrayLength = Array.getLength(bean);
			for (int i = 0; i < arrayLength; i++) {
				Object rowObj = Array.get(bean, i);
				arrayList.add(getMapListObject(rowObj));
			}
			return arrayList;
		}
		if (bean instanceof Collection) {
			ArrayList<Object> arrayList = new ArrayList<Object>();
			Iterator iterator = ((Collection) bean).iterator();
			while (iterator.hasNext()) {
				Object rowObj = iterator.next();
				arrayList.add(getMapListObject(rowObj));
			}
			return arrayList;
		}
		if (bean instanceof Map) {
			HashMap<String, Object> beanMap = new HashMap<String, Object>();
			Map map = (Map) bean;
			Iterator iterator = map.keySet().iterator();
			while (iterator.hasNext()) {
				Object key = iterator.next();
				Object rowObj = map.get(key);
				beanMap.put(key.toString(), getMapListObject(rowObj));
			}
			return beanMap;
		}
		HashMap beanMap = new HashMap();
		Class objClass = bean.getClass();
		Method[] methods = objClass.getMethods();
		for (Method method : methods) {
			try {
				String name = method.getName();
				String key = "";
				if (name.startsWith("get")) {
					key = name.substring(3);
				} else if (name.startsWith("is")) {
					key = name.substring(2);
				}
				if (key.length() > 0 && Character.isUpperCase(key.charAt(0)) && method.getParameterTypes().length == 0) {
					Object elementObj = method.invoke(bean);
					if (elementObj instanceof Class) {
						beanMap.put(StringUtil.toLowerCaseInitial(key), elementObj.toString());
					} else {
						if (elementObj instanceof Map)// 为模版中的属性值为map的做特殊处理，主要用于edit页面。目前不支持map中有map
						{
							Map map = (Map) elementObj;
							Iterator iterator = map.keySet().iterator();
							while (iterator.hasNext()) {
								Object mapkey = iterator.next();
								Object rowObj = map.get(mapkey);
								beanMap.put(StringUtil.toLowerCaseInitial(key) + "." + mapkey.toString(), getMapListObject(rowObj));
							}
						}
						beanMap.put(StringUtil.toLowerCaseInitial(key), getMapListObject(elementObj));
					}
				}
			} catch (Exception e) {
				// logger
			}
		}
		return beanMap;
	}

	public static Object toHashMap(Map<String, String> map, Object obj) {
		if (instance == null)
			instance = new ObjToMapUtil();
		return instance.getMapListObject(map, obj);
	}

	public Object getMapListObject(Map<String, String> map, Object bean) {
		if (bean == null)
			return null;
		Class clazz = bean.getClass();
		if (bean instanceof Number) {
			/*
			 * if (bean instanceof Double) { return
			 * StringUtil.doubleToString(Double.valueOf(bean.toString())); }
			 * else
			 */
			if (bean instanceof Integer) {
				return String.valueOf(bean);
			} else
				return bean;
		}
		if (bean instanceof Boolean || bean instanceof String)
			return bean;
		if (bean instanceof Date) {
			String datetime = dateFormat.format(bean);
			if (datetime.lastIndexOf("00:00:00") >= 0)
				datetime = datetime.replace("00:00:00", "");
			return datetime;
		}
		if (bean instanceof java.util.UUID) {
			return ((java.util.UUID) bean).toString();
		}
		if (clazz.isArray()) {
			ArrayList<Object> arrayList = new ArrayList<Object>();
			int arrayLength = Array.getLength(bean);
			for (int i = 0; i < arrayLength; i++) {
				Object rowObj = Array.get(bean, i);
				arrayList.add(getMapListObject(map, rowObj));
			}
			return arrayList;
		}
		if (bean instanceof Collection) {
			ArrayList<Object> arrayList = new ArrayList<Object>();
			Iterator iterator = ((Collection) bean).iterator();
			while (iterator.hasNext()) {
				Object rowObj = iterator.next();
				arrayList.add(getMapListObject(map, rowObj));
			}
			return arrayList;
		}
		if (bean instanceof Map) {
			HashMap<String, Object> beanMap = new HashMap<String, Object>();
			Map tmpMap = (Map) bean;
			Iterator iterator = tmpMap.keySet().iterator();
			while (iterator.hasNext()) {
				Object key = iterator.next();
				Object rowObj = tmpMap.get(key);
				if (map.containsKey(key))
					beanMap.put(map.get(key), getMapListObject(map, rowObj));
				else
					beanMap.put(key.toString(), getMapListObject(map, rowObj));
			}
			return beanMap;
		}
		HashMap beanMap = new HashMap();
		Class objClass = bean.getClass();
		Method[] methods = objClass.getMethods();
		for (Method method : methods) {
			try {
				String name = method.getName();
				String key = "";
				if (name.startsWith("get")) {
					key = name.substring(3);
				} else if (name.startsWith("is")) {
					key = name.substring(2);
				}
				if (key.length() > 0 && Character.isUpperCase(key.charAt(0)) && method.getParameterTypes().length == 0) {
					Object elementObj = method.invoke(bean);
					if (elementObj instanceof Class) {
						if (map.containsKey(key.toLowerCase()))
							beanMap.put(map.get(StringUtil.toLowerCaseInitial(key)), elementObj.toString());
						else
							beanMap.put(StringUtil.toLowerCaseInitial(key), elementObj.toString());
					} else {
						if (map.containsKey(key.toLowerCase()))
							beanMap.put(map.get(StringUtil.toLowerCaseInitial(key)), getMapListObject(map, elementObj));
						else
							beanMap.put(StringUtil.toLowerCaseInitial(key), getMapListObject(map, elementObj));
					}
				}
			} catch (Exception e) {
				// logger
			}
		}
		return beanMap;
	}
}
